dnl                    -*- Autoconf -*-
dnl Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)
dnl TODO change for devel package
define([PDFEDIT_VERSION], patsubst(esyscmd(./getversion -v),[]))
AC_INIT([PDFedit], PDFEDIT_VERSION, 
	[BUGTRACKER - http://pdfedit.petricek.net/bt/ ],[pdfedit])
dnl FIXME what do we use src/utils/aconf.h for? No includes according to
dnl grep. Can we remove it?
AC_CONFIG_HEADERS(src/utils/aconf.h src/xpdf/xpdf-aconf.h)

m4_include([config/env.m4])
m4_include([config/macro.m4])
m4_include([config/boost_base.m4])
m4_include([config/cppunit.m4])
m4_include([config/freetype2.m4])
m4_include([config/ax_check_zlib.m4])
m4_include([config/ax_boost_program_options.m4])

m4_include([config/xpdf.m4])

dnl Checks for programs. (c++ compiler,...)
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_RANLIB

dnl This has problems with some autoconf versions which require and test
dnl install-sh script to be present in current directory. Actually we don't
dnl depend on it and it is one of just-to-be-sure tests. Maybe we can enable
dnl later if we find it necessary.
dnl AC_PROG_MKDIR_P

AC_PROG_LN_S

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([limits.h stdlib.h string.h unistd.h])

dnl Checks for typedefs, structures, and compiler characteristics.
dnl TODO where do we use output?
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_STRUCT_TM
AX_CHECK_ZLIB

dnl Checks for boost
AX_BOOST_BASE

dnl fake-install-tools can be used to installation tools (COPY, DEL_*, 
dnl SYMLINK, etc) by those which are safe for testing - don't touch
dnl anything and instead logs all actions - tools/fake_install_tools.sh
dnl script with the first parameter as original operation
AC_ARG_WITH([fake-install],
	    AS_HELP_STRING([--with-fake-install=YES|NO],
			   [Only log actions that would be done during make 
			    {un}install, instead of doing them (to the 
			    /tmp/fake_install_tools.log). This is only 
			    for testing purposes.]
	     ),
	     [
	      	fake_install="$withval"
	     ],
	     [fake_install="NO"]
	      )
if test "x$fake_install" = "xYES"
then
	AC_MSG_NOTICE("Using fake-install-tools.")
	COPY='sh $(ROOT)/tools/fake_install_tools.sh COPY'
	DEL_FILE='sh $(ROOT)/tools/fake_install_tools.sh DEL_FILE'
	DEL_FORCE_DIR='sh $(ROOT)/tools/fake_install_tools.sh DEL_FORCE_DIR'
	DEL_DIR='sh $(ROOT)/tools/fake_install_tools.sh DEL_DIR'
	SYMLINK='sh $(ROOT)/tools/fake_install_tools.sh SYMLINK'
	MKDIR='sh $(ROOT)/tools/fake_install_tools.sh MKDIR'
	CHK_DIR_EXISTS='sh $(ROOT)/tools/fake_install_tools.sh CHK_DIR_EXISTS'
fi


dnl Default (and configurable) values for files/directories manipulation 
dnl programs (you can overwrite them by ./configure NAME=value)
CHK_OR_DEFINE_VAR(COPY, "cp -f")
CHK_OR_DEFINE_VAR(DEL_FILE, "rm -f")
CHK_OR_DEFINE_VAR(DEL_DIR, "sh \$(ROOT)/tools/rmdir.sh")
CHK_OR_DEFINE_VAR(DEL_FORCE_DIR, "rm -rf")
CHK_OR_DEFINE_VAR(SYMLINK, "ln -s")
CHK_OR_DEFINE_VAR(MOVE, "mv -f")
CHK_OR_DEFINE_VAR(CHK_DIR_EXISTS, "test -d")
CHK_OR_DEFINE_VAR(MKDIR, "mkdir -p")
AC_SUBST(COPY)
AC_SUBST(DEL_FILE)
AC_SUBST(DEL_DIR)
AC_SUBST(DEL_FORCE_DIR)
AC_SUBST(SYMLINK)
AC_SUBST(MOVE)
AC_SUBST(CHK_DIR_EXISTS)
AC_SUBST(MKDIR)


dnl compiler options which might be set by environment viariable
dnl TODO check whether this is ok also for other platforms
CHK_OR_DEFINE_VAR(ARCH, "")
CHK_OR_DEFINE_VAR(C_EXTRA, "-fmessage-length=0 -D_FORTIFY_SOURCE=2 -fno-strict-aliasing")
CHK_OR_DEFINE_VAR(EXTRA_UTILS_CFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_KERNEL_CFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_TESTS_CFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_XPDF_CFLAGS, "")
CHK_OR_DEFINE_VAR(CXX_EXTRA, "-fmessage-length=0 -D_FORTIFY_SOURCE=2 -fno-strict-aliasing -fexceptions")
CHK_OR_DEFINE_VAR(EXTRA_UTILS_CXXFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_KERNEL_CXXFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_TESTS_CXXFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_GUI_CXXFLAGS, "")
CHK_OR_DEFINE_VAR(EXTRA_XPDF_CXXFLAGS, "")
AC_SUBST(ARCH)
AC_SUBST(C_EXTRA)
AC_SUBST(EXTRA_UTILS_CFLAGS)
AC_SUBST(EXTRA_KERNEL_CFLAGS)
AC_SUBST(EXTRA_TESTS_CFLAGS)
AC_SUBST(EXTRA_XPDF_CFLAGS)
AC_SUBST(CXX_EXTRA)
AC_SUBST(EXTRA_UTILS_CXXFLAGS)
AC_SUBST(EXTRA_KERNEL_CXXFLAGS)
AC_SUBST(EXTRA_TESTS_CXXFLAGS)
AC_SUBST(EXTRA_GUI_CXXFLAGS)
AC_SUBST(EXTRA_XPDF_CXXFLAGS)

AC_ARG_ENABLE(portability-flags,
	      [AS_HELP_STRING([--enable-portability-flags],
			      [Turn on compilation flags which force the c/c++ standard constructs and proper portability (this includes -std=c99, -std=c++98, -ansi, -posix, -pedantic). Enabled by default])],
			      ,
			      [enable_portability_flags=yes])
AC_MSG_CHECKING(whether c/c++ and portability flags are forced)
if test "x$enable_portability_flags" = "xyes"
then
	C_PORTABILITY_FLAGS="-posix -ansi -std=c99 -pedantic -D_XOPEN_SOURCE=500"
	CXX_PORTABILITY_FLAGS="-posix -ansi -std=c++98 -D_XOPEN_SOURCE=500"
	AC_MSG_RESULT(yes)
else
	C_PORTABILITY_FLAGS=""
	CXX_PORTABILITY_FLAGS=""
	AC_MSG_RESULT(no)
fi
AC_SUBST(C_PORTABILITY_FLAGS)
AC_SUBST(CXX_PORTABILITY_FLAGS)

dnl Default (and configurable) values for libraries
CHK_OR_DEFINE_VAR(AR, "ar cqs")
CHK_OR_DEFINE_VAR(LIBPREFIX, "lib")
AC_SUBST(AR)
AC_SUBST(LIBPREFIX)


AC_ARG_ENABLE(stack-protector,
	      [AS_HELP_STRING([--enable-stack-protector],
			      [Turn on stack protector (disabled by default)])],
			      ,
			      [enable_stack_protector=no])
AC_MSG_CHECKING(whether we use stack protector)
if test "x$enable_stack_protector" != "xno"
then
	STACK_PROTECTOR_FLAGS="-fstack-protector"
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
	STACK_PROTECTOR_FLAGS=""
fi
AC_SUBST(STACK_PROTECTOR_FLAGS)

AC_ARG_ENABLE(release,
	      [AS_HELP_STRING([--enable-release],
			      [Turn on compiler optimizations, turn off 
			       debugging symbols (enabled by default, disable 
						  with --disable-release)])],
			      ,
			      [enable_release=yes])
AC_SUBST(enable_release)

dnl if we want release then optimalization is turned on and we can optionally 
dnl add debug information. Otherwise we will turn off optimizations and allways
dnl get debug info
AC_ARG_ENABLE(debug-info,
	      [AS_HELP_STRING([--enable-debug-info],
			      [Adds debug information (used only for release and
			       it is disabled by default)])],
			      ,
			      [enable_debug_info=no])
AC_MSG_CHECKING(whether we are releasing)
E_RELEASE="no"
if test "x$enable_release" = "xyes"; then
	AC_MSG_RESULT(true)
	OPTIM="$OPTIM -O2"
	AC_MSG_CHECKING(whether debug info is included)
	if test "x$enable_debug_info" = "xyes"
	then
		DEBUG="$DEBUG -g"
		AC_MSG_RESULT(yes)
	else
		AC_MSG_RESULT(no)
	fi
	E_RELEASE="yes"
	WARN=""
else
	AC_MSG_RESULT(no)
	OPTIM="$OPTIM -O0"
	dnl maybe asserts and stuff like that?
	DEBUG="$DEBUG -g -DDEBUG"
	WARN="$WARN -W -Wall -Wconversion -Wno-unused-variable \
	   -Wunused-function -Wno-write-strings \
	   -Wunused-macros"

fi
AC_SUBST(OPTIM)
AC_SUBST(DEBUG)
AC_SUBST(E_RELEASE)
AC_SUBST(WARN)

dnl Enable observer debugging (disabled by default)
AC_ARG_ENABLE(observer-debug,
[AS_HELP_STRING([--enable-observer-debug],
		[Turn on observer debugging (disabled by default)])],
		,
		[enable_observer_debug=no])
AC_MSG_CHECKING(whether to enable observer debugging)
if test "x$enable_observer_debug" = "xyes"; then
	OBSERVER_CFLAGS="$OBSERVER_CFLAGS -DOBSERVER_DEBUG"
	OBSERVER_CXXFLAGS="$OBSERVER_CXXFLAGS -DOBSERVER_DEBUG"
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(OBSERVER_CFLAGS)
AC_SUBST(OBSERVER_CXXFLAGS)

dnl Checks for library functions.
AC_FUNC_ERROR_AT_LINE
AC_FUNC_MALLOC
AC_FUNC_STAT
AC_FUNC_STRFTIME
AC_CHECK_FUNCS([atexit floor ftruncate localtime_r memset mkdir strdup strerror strpbrk strstr])

dnl XPdf code requires >=2.0.5
dnl TODO check for version
AC_CHECK_FT2

smr_CHECK_LIB(t1, t1, [Type 1 font rasterizer], 
              T1_InitLib, t1lib.h,
              -lm, $X_CFLAGS)
AC_SUBST(t1_LIBS)
AC_SUBST(t1_CFLAGS)

dnl We don't need to have explicit --enable-man-doc option so we keep it
dnl implicit and if some option want to include man pages, it simply sets
dnl this to true and the rest is done later in this script
enable_man_doc="no"

dnl TODO maybe be can add also generic pdfedit-dev package which will install
dnl pdfedit-core-dev and pdfedit-gui-dev (which will contain gui reusable code
dnl e.g. scripting)

dnl Sets DEV_TARGET for pdfedit-core-dev package
AC_ARG_ENABLE(pdfedit-core-dev,
	      AS_HELP_STRING([--enable-pdfedit-core-dev],
			     [Builds and prepars PDFedit-core-dev package which can be
			      used by 3rd party project separately from gui frontend
			      (this includes static libraries and header files)]),
			      ,
			      [enable_pdfedit_core_dev=no])
AC_MSG_CHECKING(whether we want to prepare pdfedit-core-dev package)
if test "x$enable_pdfedit_core_dev" = "xyes"
then
	AC_MSG_RESULT(yes)
	INSTALL_TARGET="$INSTALL_TARGET install-core-dev"
	UNINSTALL_TARGET="$UNINSTALL_TARGET uninstall-core-dev"

else
	AC_MSG_RESULT(no)
fi

dnl Sets GUI_TARGET and qt specific stuff
AC_ARG_ENABLE(gui,
	      [AS_HELP_STRING([--enable-gui],
			      [Builds gui frontend (enabled by default, use 
			       --disable-gui to disable it)])],
			      ,
			      [enable_gui=yes])

AC_MSG_CHECKING(whether we want to build PDFedit gui)
dnl Check QT environment if we want GUI
dnl QSA_DIR is set to qt3 by default. Note that this is important because
dnl we need at least some qsa directory for src/Makefile and its clean target
dnl gui implies also man pages
QSA_DIR="$QSA_DIR qsa"
if test "x$enable_gui" = "xyes"; then
	AC_MSG_RESULT(yes)
	GUI_TARGET="$GUI_TARGET pdfedit"
	INSTALL_TARGET="$INSTALL_TARGET install-gui"
	UNINSTALL_TARGET="$UNINSTALL_TARGET uninstall-gui"

	dnl QTDIR is mandatory
	CHK_ENV_VAR([QTDIR],
		    [QTDIR environment variable must be set])
	dnl QMAKESPEC is optional but recomended
	CHK_ENV_VAR_NOTICE([QMAKESPEC],
			   [QMAKESPEC environment variable is not set 
			    - default will be used.])

	AC_MSG_CHECKING(for QT qmake)
	CHK_EXECUTABLE([qmake],[qmake qmake-qt4],[${QTDIR}/bin /usr/bin /usr/local/bin /usr/lib/qt4/bin],[
		dnl check the qmake we found is for correct QT version (4.x.x)
		isqmake4=`$testbin --version 2>&1 | grep '4\.[[0-9]][[0-9]]*\.[[0-9]][[0-9]]*'`
		if test -z "$isqmake4"
		then
			testbin=""
		fi
	],[(only for platforms where qmake is not installed under QTDIR/bin, /usr/bin, /usr/local/bin, /usr/lib/qt4/bin or in PATH).])
	if test -z "${qmake_bin}"
	then
		AC_MSG_ERROR(unable to find qmake for QT3)
	fi
	QMAKE="$QMAKE ${qmake_bin}"
	AC_MSG_RESULT(Using QT4 with qmake=${qmake_bin})
	AC_SUBST(QMAKE)

	dnl check for lrelease
	AC_MSG_CHECKING(for QT lrelease)
	CHK_EXECUTABLE([lrelease],[lrelease],[${QTDIR}/bin /usr/bin /usr/local/bin /usr/lib/qt4/bin],[],
		       [(Only for platforms where lrelease is not installed under QTDIR/bin, /usr/bin, /usr/local/bin, /usr/lib/qt4/bin or in PATH).])
	if test -z "${lrelease_bin}"
	then
		AC_MSG_ERROR(unable to find lrelease localization binary for QT3)
	fi
	LRELEASE="$LRELEASE ${lrelease_bin}"
	AC_MSG_RESULT(Using lrelease=${lrelease_bin})
	AC_SUBST(LRELEASE)

	dnl enable man pages
	enable_man_doc="yes"
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(GUI_TARGET)
AC_SUBST(QSA_DIR)

dnl Exports installation targets - install-no is used if no specific
dnl installation target was configured
if test "x$INSTALL_TARGET" = "x"
then
	INSTALL_TARGET="$INSTALL_TARGET install-no"
fi
if test "x$UNINSTALL_TARGET" = "x"
then
	UNINSTALL_TARGET="$UNINSTALL_TARGET uninstall-no"
fi
AC_SUBST(INSTALL_TARGET)
AC_SUBST(UNINSTALL_TARGET)

AC_ARG_WITH([root-dir],
	    AS_HELP_STRING([--with-root-dir=DIR], 
			   [Install files under different root directory (path to prepend before
			    the prefix, empty by default - use if you are packager and want to create
			    package, or if you are installing into chrooted environment).]
			   ),
			   [
				root_dir="$withval/"
			   ],
			   [root_dir=""]
	    )
AC_SUBST(root_dir)

AC_ARG_WITH([parallel-make],
	    AS_HELP_STRING([--with-parallel-make=auto|off|number],
		     [Sets MAKEFLAGS so that make will use parallel building when possible. This option
		     is useful when you have more CPUs/cores because each core can compile independently
		     and the compilation will take shorter time. You can use auto (default) which will
		     find out the number of CPU available and use this value, off for no parallelism
		     or number to specify the value explicitly.]
	     ),
	     [
	      	parallel_make_set="$withval"
	     ],
	     [parallel_make_set="auto"]
	      )

parallel_make=""
if test "i$parallel_make_set" = "iauto"
then
{
	AC_MSG_CHECKING(for /proc/cpuinfo )
	if test -f /proc/cpuinfo
	then
	{
		dnl /proc/cpuinfo is available in Linux and cygwin systems
		CPU_NUM=`grep "^processor" /proc/cpuinfo | wc -l`
		if test $CPU_NUM -gt 1
		then
			AC_MSG_RESULT($CPU_NUM CPUs/Cores available - using $CPU_NUM parallel jobs)
			parallel_make="-j$CPU_NUM"
		else
			AC_MSG_RESULT(Only one CPU available - no parallelism)
		fi
	}
	else
	{
		AC_MSG_RESULT(not available)
		AC_MSG_CHECKING(for sysctl )
		dnl sysctl is available on BSD systems
		CPU_NUM=`/bin/sh -c 'sysctl -n hw.ncpu' 2>/dev/null`
		if test -n "$CPU_NUM"
		then
		{
			if test $CPU_NUM -gt 1
			then
				AC_MSG_RESULT($CPU_NUM CPUs/Cores available - using $CPU_NUM parallel jobs)
				parallel_make="-j$CPU_NUM"
			else
				AC_MSG_RESULT(Only one CPU available - no parallelism)
			fi
		}
		else
			AC_MSG_RESULT(not available)
			dnl TODO checks for other platforms
		fi
	}
	fi
}
else
{
	AC_MSG_CHECKING(--with-parallel-make value)
	if test "i$parallel_make_set" = "ioff"
	then
		AC_MSG_RESULT(No parallelism)
	else
	{
		if echo $parallel_make_set | grep '^ *[[0-9]][[0-9]]* *$' >/dev/null 2>&1
		then
			AC_MSG_RESULT(Using $parallel_make_set)
			parallel_make="-j$parallel_make_set"
		else
			AC_MSG_ERROR($parallel_make_set is not valid)
		fi
	}
	fi
}
fi
AC_SUBST(parallel_make)

dnl TODO handle also dev package (exclusive with gui case)

dnl Sets TESTS_TARGET and cppunit stuff, if we want to have 
dnl also kernel tests
AC_ARG_ENABLE(kernel-tests,
	      [AS_HELP_STRING([--enable-kernel-tests],
			      [Compiles also kernel tests 
			       (disabled by default)])],
			      ,
			      [enable_kernel_tests=no])
AC_MSG_CHECKING(whether we want to compile kernel tests too)
if test "x$enable_kernel_tests" = "xyes"; then
	AC_MSG_RESULT(yes)
	KERNEL_TESTS_TARGET="$KERNEL_TESTS_TARGET kernel_tests"
	dnl FIXME check the version - it is required by macro - I have used some
	dnl random one
	AM_PATH_CPPUNIT(1.10.0)
	if test "x${no_cppunit}" != "x"
	then
		AC_MSG_ERROR(Unable to find CPPUnit required by --enable-kernel-tests)
	fi
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(KERNEL_TESTS_TARGET)

dnl add man pages creation and installation target only if requiered by others
dnl options
if test "x$enable_man_doc" = "xyes"
then
	MAN_TARGET="$MAN_TARGET doc_man"
	MAN_INSTALL_TARGET="$MAN_INSTALL_TARGET doc_man_install"
fi
AC_SUBST(MAN_TARGET)
AC_SUBST(MAN_INSTALL_TARGET)

AC_ARG_ENABLE(doxygen-doc,
	      [AS_HELP_STRING([--enable-doxygen-doc],
			      [Creates doxygen documentation (extracted from sources)])],
			      ,
			      [enable_doxygen_doc=no])
AC_MSG_CHECKING(whether we want to build doxygen documentation)
if test "x$enable_doxygen_doc" = "xyes"
then
	AC_MSG_RESULT(yes)
	AC_MSG_CHECKING(for doxygen binary)
	CHK_EXECUTABLE(doxygen,doxygen,[/usr/bin /usr/local/bin],[])
	if test -z ${doxygen_bin}
	then
		AC_MSG_ERROR(unable to find doxygen binary)
	fi
	AC_MSG_RESULT(found at ${doxygen_bin})
	DOXYGEN_BIN="$DOXYGEN_BIN ${doxygen_bin}"
	DOXYGEN_TARGET="$DOXYGEN_TARGET doc_doxygen"
	DOXYGEN_INSTALL_TARGET="$DOXYGEN_INSTALL_TARGET doc_doxygen_install"
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(DOXYGEN_TARGET)
AC_SUBST(DOXYGEN_INSTALL_TARGET)
AC_SUBST(DOXYGEN_BIN)

dnl Enable building advanced documentation?
AC_ARG_ENABLE(advanced-doc,
	      [AS_HELP_STRING([--enable-advanced-doc],
			      [Creates advanced documentation (advanced 
			       programming and design description)])],
			      ,
			      [enable_advanced_doc=no])
AC_MSG_CHECKING(whether we want to build advanced documentation)
if test "x$enable_advanced_doc" = "xyes"
then
	AC_MSG_RESULT(yes)
	ADVANCED_TARGET="$ADVANCED_TARGET doc_advanced"
	ADVANCED_INSTALL_TARGET="$ADVANCED_INSTALL_TARGET doc_advanced_install"
	xsl_tools_required=yes
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(ADVANCED_TARGET)
AC_SUBST(ADVANCED_INSTALL_TARGET)

dnl Enable building user manual?
AC_ARG_ENABLE(user-manual,
	      [AS_HELP_STRING([--enable-user-manual],
			      [Creates user manual])],
			      ,
			      [enable_user_manual=no])
AC_MSG_CHECKING(whether we want to build user manual)
if test "x$enable_user_manual" = "xyes"
then
	AC_MSG_RESULT(yes)
	USER_MANUAL_TARGET="$USER_MANUAL_TARGET doc_user"
	USER_MANUAL_INSTALL_TARGET="$USER_MANUAL_INSTALL_TARGET doc_user_install"
	xsl_tools_required=yes
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(USER_MANUAL_TARGET)
AC_SUBST(USER_MANUAL_INSTALL_TARGET)

if test "x$xsl_tools_required" = "xyes"
then
	dnl check for xsltproc
	AC_MSG_CHECKING(for xsltproc binary)
	CHK_EXECUTABLE(xsltproc,xsltproc,[/usr/bin /usr/local/bin],[])
	if test -z ${xsltproc_bin}
	then
		AC_MSG_ERROR(unable to find xsltproc binary)
	else 
		AC_MSG_RESULT(found at ${xsltproc_bin})
	fi

	dnl check for docbook xsl
	dnl TODO create macro for detecting
	AC_MSG_CHECKING(for docbook xsl)
	docbook_xsl=""
	for i in `cat doc/tools/docbook_xslt_paths`
	do
		if test -f $i/docbook.xsl
		then
			docbook_xsl=$i/docbook.xsl
			break
		fi
	done
	if test -z ${docbook_xsl}
	then
		AC_MSG_ERROR(unable to find docbook.xsl stylesheet)
	else
		AC_MSG_RESULT(found at ${docbook_xsl})
	fi
	dnl TODO: if xsltproc and stylesheet is missing, docbook2html from jade can be used
fi

dnl Enable building tools?
AC_ARG_ENABLE(tools,
	      [AS_HELP_STRING([--enable-tools],
			      [Enables tools building])],
			      ,
			      [enable_tools=no])
AC_MSG_CHECKING(whether we want to build tools)
if test "x$enable_tools" = "xyes"
then
	AC_MSG_RESULT(yes)
	boost_program_options_required=yes
	png_required=yes
	if test "x$enable_pdfedit_core_dev" != "xyes"
	then
		AC_MSG_ERROR(--enable pdfedit-core-dev is required for tools)
	fi
	TOOLS_TARGET="$TOOLS_TARGET tools"
else
	AC_MSG_RESULT(no)
fi
AC_SUBST(TOOLS_TARGET)

if test "x$boost_program_options_required" = "xyes"
then
	AC_MSG_CHECKING(for boost-program-options library)
	AX_BOOST_PROGRAM_OPTIONS
fi

png_LIBS=""
png_HEADERS=""
if test "x$png_required" = "xyes"
then
	AC_MSG_CHECKING(for png library)
	AC_LANG_SAVE
	AC_LANG_C
	AC_CHECK_LIB(png, png_check_sig, [pnglib=yes])
	AC_CHECK_HEADERS(png.h, [pngheader=yes])
	AC_LANG_RESTORE
	if test "x$pnglib" = "x" 
	then
		AC_MSG_ERROR(libpng is not found)
	fi
	if test "x$pngheader" = "x"
	then
		AC_MSG_ERROR(png headers is missing)
	fi
	AC_MSG_RESULT(yes)

	dnl keep the header specific settings empty and library simply
	dnl for now but let's integrate it into the rest of the building system
	png_LIBS="-lpng"
fi
AC_SUBST(png_LIBS)
AC_SUBST(png_CFLAGS)

AC_CONFIG_FILES([Makefile.flags Makefile.rules])

dnl
dnl XPDF specific stuff taken from the original xpdf distribution
dnl

AC_PATH_XTRA
AC_HEADER_DIRENT

dnl ##### Switch over to C++.  This will make the checks below a little
dnl ##### bit stricter (requiring function prototypes in include files).
dnl ##### (99% of xpdf is written in C++.)
AC_LANG_CPLUSPLUS

dnl ##### Look for rewinddir.
AC_CHECK_FUNCS(rewinddir)
if test $ac_cv_func_rewinddir = no; then
  AC_CHECK_LIB(cposix, rewinddir)
fi

dnl ##### Checks for library functions.
AC_CHECK_FUNCS(popen)
dnl # This should use 'AC_CHECK_FUNCS(mkstemp)' but that fails if
dnl # the mkstemp exists in the library but isn't declared in the
dnl # include file (e.g., in cygwin 1.1.2).
AC_CACHE_CHECK([for mkstemp],
xpdf_cv_func_mkstemp,
[AC_TRY_LINK([#include <stdlib.h>
#include <unistd.h>],
[mkstemp("foo");],
xpdf_cv_func_mkstemp=yes, xpdf_cv_func_mkstemp=no)])
if test "$xpdf_cv_func_mkstemp" = yes; then
  AC_DEFINE(HAVE_MKSTEMP)
fi
dnl # Check for mkstemps, just like mkstemp.
AC_CACHE_CHECK([for mkstemps],
xpdf_cv_func_mkstemps,
[AC_TRY_LINK([#include <stdlib.h>
#include <unistd.h>],
[mkstemps("foo", 0);],
xpdf_cv_func_mkstemps=yes, xpdf_cv_func_mkstemps=no)])
if test "$xpdf_cv_func_mkstemps" = yes; then
  AC_DEFINE(HAVE_MKSTEMPS)
fi

dnl ##### Back to C for the library tests.
AC_LANG_C

dnl ##### Check for fseeko/ftello or fseek64/ftell64
dnl The LARGEFILE and FSEEKO macros have to be called in C, not C++, mode.
AC_SYS_LARGEFILE
AC_FUNC_FSEEKO
AC_CHECK_FUNCS(fseek64, xpdf_cv_func_fseek64=yes, xpdf_cv_func_fseek64=no)
AC_CHECK_FUNCS(ftell64, xpdf_cv_func_ftell64=yes, xpdf_cv_func_ftell64=no)
if test "$xpdf_cv_func_fseek64" = yes -a "$xpdf_cv_func_ftell64" = yes; then
  AC_DEFINE(HAVE_FSEEK64)
fi

if test "x${t1_LIBS}" != "x" 
then
	AC_DEFINE(HAVE_T1LIB_H)
fi

if test "x${FT2_LIBS}" != "x"
then
  AC_DEFINE(HAVE_FREETYPE_FREETYPE_H)
  AC_DEFINE(HAVE_SPLASH)
fi

dnl ##### Write the makefiles.
AC_OUTPUT(src/xpdf/Makefile src/xpdf/goo/Makefile src/xpdf/fofi/Makefile src/xpdf/splash/Makefile src/xpdf/xpdf/Makefile )

echo ""
echo "Configuration complete"
echo " Building in release mode      : $enable_release"
if test  "$enable_release" = "yes"
then
	echo " Include debugging information : $enable_debug_info"
fi
echo " Enable observer debugging     : $enable_observer_debug"
echo " Build man pages               : $enable_man_doc"
echo " Build user manual             : $enable_user_manual"
echo " Build doxygen documentation   : $enable_doxygen_doc"
echo " Build advanced documentation  : $enable_advanced_doc"
echo " Build kernel tests            : $enable_kernel_tests"
echo " Build GUI                     : $enable_gui"
echo " Build pdfedit-core-dev        : $enable_pdfedit_core_dev"
echo " Build tools                   : $enable_tools"
echo
echo "Installation summary:"
echo " Root directory                : $root_dir"
echo " Install into                  : $prefix"
echo " Install pdfedit-core-dev      : $enable_pdfedit_core_dev"
echo " Install gui                   : $enable_gui"
echo ""
echo "Type 'make' to build $PACKAGE_STRING"
echo ""
